/*
 * ProjectFrame.java
 *
 * Created on 25 avr. 2012, 14:25:49
 */

package traffic.light.simulator.view;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import traffic.light.simulator.town.Town;
import traffic.light.simulator.town.Town.TownNotValidException;
import traffic.light.simulator.town.unity.CrossRoadType;

/**
 *
 */
public class ProjectFrame extends javax.swing.JFrame {

    /** Creates new form ProjectFrame */
    public ProjectFrame() {
        initComponents();
        this.jButton2.setEnabled(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        town = new Town(10);
        jPanel2 = new javax.swing.JPanel();
        StartJBtn = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        town.setPreferredSize(new java.awt.Dimension(((Town)this.town).getSizeOfTown(),((Town)this.town).getSizeOfTown()));

        javax.swing.GroupLayout townLayout = new javax.swing.GroupLayout(town);
        town.setLayout(townLayout);
        townLayout.setHorizontalGroup(
            townLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 10, Short.MAX_VALUE)
        );
        townLayout.setVerticalGroup(
            townLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 10, Short.MAX_VALUE)
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        StartJBtn.setText("start");
        StartJBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                StartJBtnActionPerformed(evt);
            }
        });

        jButton2.setText("pause");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(StartJBtn, javax.swing.GroupLayout.DEFAULT_SIZE, 127, Short.MAX_VALUE)
            .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 127, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(StartJBtn)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton2)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(town, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(town, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void StartJBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_StartJBtnActionPerformed
        try {
            ((Town)this.town).run();
            this.StartJBtn.setEnabled(false);
            this.jButton2.setEnabled(true);
        } catch (TownNotValidException ex) {
            //message interface graphique
        }
    }//GEN-LAST:event_StartJBtnActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        ((Town)this.town).pause();
        if(this.jButton2.getText().equalsIgnoreCase("Restart"))
            this.jButton2.setText("Pause");
        else
            this.jButton2.setText("Restart");
    }//GEN-LAST:event_jButton2ActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        try {
            File f = new File(ClassLoader.getSystemResource("traffic/light/simulator/data/Town.txt").toURI());
            FileReader filer = new FileReader(f);
            BufferedReader br= new BufferedReader(filer);
            String fileInString = "";

            fileInString+= br.readLine()+"\n";
            
            int nbOfElement=new Integer(br.readLine());
            fileInString+=nbOfElement+"\n";
            for(int l=0;l<nbOfElement;l++){
                for(int c=0;c<nbOfElement;c++){
                    fileInString+=CrossRoadType.getInt(((Town)this.town).getTownUnity(l,c).getCrossRoadType())+"";
                    if(c+1==nbOfElement)
                        fileInString+="\n";
                    else
                        fileInString+=",";
                }
            }
            br.close();
            filer.close();
            

            FileWriter filew = new FileWriter(f);           
            BufferedWriter bw= new BufferedWriter(filew);
            bw.write(fileInString);
            bw.close();
            filew.close();
        } catch (Exception ex) {
            System.out.println(ex);
        }
    }//GEN-LAST:event_formWindowClosing


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton StartJBtn;
    private javax.swing.JButton jButton2;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel town;
    // End of variables declaration//GEN-END:variables

}
